package superuser

import (
	"NovelBook/dao"
	"NovelBook/models/res"
	"context"
	"time"

	"github.com/redis/go-redis/v9"
)

func GetThroughputInfo() (res.Throughput, error) {
	ctx := context.Background()
	var Info res.Throughput
	var err error
	// 获取今天的信息
	today := time.Now().Format("2006-01-02")

	// 访问量，这里重写了Get方法来获取int值
	if Info.Visits, err = dao.Rdb.Get(ctx, "visits:"+today).Int(); err != nil {
		if err == redis.Nil {
			today := time.Now().Format("2006-01-02")
			remainingTime := time.Until(time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 23, 59, 59, 0, time.Now().Location()))
			if err := dao.Set("visits:"+today, 0, remainingTime); err != nil {
				return Info, err
			}
		} else {
			return Info, err
		}
	}

	// 借(B)还（R）书人数
	if Info.BRNumber, err = dao.Rdb.Get(ctx, "br_numbers:"+today).Int(); err != nil {
		if err == redis.Nil {
			today := time.Now().Format("2006-01-02")
			remainingTime := time.Until(time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 23, 59, 59, 0, time.Now().Location()))
			if err := dao.Set("br_numbers:"+today, 0, remainingTime); err != nil {
				return Info, err
			}
		} else {
			return Info, err
		}
	}

	return Info, nil
}
